package com.ynny.service;

import com.ynny.bean.Course;
import com.ynny.bean.Score;
import com.ynny.dao.CourseManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Component("courseManage")
public class CourseManageImpl implements CourseManage {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void updataCourse(String courseName) {
        String sql = "update course set"+"courseName"+ "=" +courseName+"="+"-1";
        int num = jdbcTemplate.update(sql);
        if (num<=0)
            System.out.println("更新待选课程记录失败");

    }

    @Override
    public void queryCourse() {
        String sql = "select * from courses";
        RowMapper<Course> rowMapper;
        rowMapper= new BeanPropertyRowMapper<Course>(Course.class);
        jdbcTemplate.queryForObject(sql, rowMapper);
        boolean Course = false;
        System.out.println(Course);

    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false,isolation = Isolation.DEFAULT)
    public void selectCourse(String number,String courseName) {
        updataCourse(courseName);
        queryCourse();
        jdbcTemplate.execute("create table if not exists optional_courses("

                +"number varchar(20),courseName varchar(20), Score double)");

        String sql = "insert into optional_courses (number,courseName,score) values(?,?,?)";

        jdbcTemplate.update(sql,number,courseName,0.0);
        System.out.println("查询选课信息记录");
        sql="select * from optional_courses where number=?";

        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql,number);

        for (Map<String, Object> map : maps)
            System.out.println(map);


    }
}
